<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Doctor;

/**
 * DoctorSearch represents the model behind the search form about `backend\models\Doctor`.
 */
class DoctorSearch extends Doctor
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'u_id', 'status', 'score', 'response_times', 'follow_numbers'], 'integer'],
            [['name', 'title', 'head_pic', 'live_pic', 'introduce', 'birthday', 'alipay', 'address', 'work_unit'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Doctor::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);
        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'u_id' => $this->u_id,
            'birthday' => $this->birthday,
            'status' => $this->status,
            'score' => $this->score,
            'response_times' => $this->response_times,
            'follow_numbers' => $this->follow_numbers,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'head_pic', $this->head_pic])
            ->andFilterWhere(['like', 'live_pic', $this->live_pic])
            ->andFilterWhere(['like', 'introduce', $this->introduce])
            ->andFilterWhere(['like', 'alipay', $this->alipay])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'work_unit', $this->work_unit]);

        return $dataProvider;
    }


    public function mySearch($params)
    {
        $field = [
            'd.*','u.username'
        ];
        $data  = (new \yii\db\Query())
            ->select($field)
            ->from('{{%doctor}} as d')
            ->join('left join','{{%user}} as u','d.u_id = u.id')
            ->all();

        return $data;
    }
}
